package com.bytedance.frameworks.baselib.network.http.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import inline.NetworkDemo;

public final class HttpQueryHelper {
    public final Map<String,String> queryMap;
    public String website;

    public HttpQueryHelper() {
        this.queryMap = new LinkedHashMap();
        this.website = null;
    }

    public HttpQueryHelper(String website) {
        this.queryMap = new LinkedHashMap();
        this.website = website;
    }

    public final String getEncodeQuery() {
        if (this.queryMap.isEmpty()) {
            return this.website;
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            Iterator<Entry<String, String>> iterator = this.queryMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, String> entry = iterator.next();
                String encodedName = NetworkDemo.urlEncode(entry.getKey(), "UTF-8");
                String value = entry.getValue();
                String encodedValue = value != null ? NetworkDemo.urlEncode(value, "UTF-8") : "";
                if (stringBuilder.length() > 0) {
                    stringBuilder.append("&");
                }
                stringBuilder.append(encodedName);
                stringBuilder.append("=");
                stringBuilder.append(encodedValue);
            }

            String encodeQuery = stringBuilder.toString();
            if (this.website == null) {
                return encodeQuery;
            }

            if (this.website.length() == 0) {
                return encodeQuery;
            }

            if (this.website.indexOf('?') >= 0) {
                return this.website + "&" + encodeQuery;
            }
            return this.website + "?" + encodeQuery;
        }
    }

    public final void put(String name, double value) {
        this.queryMap.put(name, String.valueOf(value));
    }

    public final void put(String arg3, int arg4) {
        this.queryMap.put(arg3, String.valueOf(arg4));
    }

    public final void put(String arg3, long arg4) {
        this.queryMap.put(arg3, String.valueOf(arg4));
    }

    public final void put(String arg2, String arg3) {
        this.queryMap.put(arg2, arg3);
    }

    public final String toString() {
        return this.getEncodeQuery();
    }
}

